package org.liveboardgames.agricola.view.component
{
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.core.DragSource;
	import mx.managers.DragManager;
	
	import org.liveboardgames.agricola.vo.farm.FarmElement;

	public class StableImage extends Image
	{
		private var _color:String;

		public function StableImage(color:String = null)
		{
			super();
			this._color = color;
			this.setStyle("horizontalCenter", "0");
			this.setStyle("verticalCenter", "0");
			this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
			updateSource();
		}

		override public function set width(value:Number):void
		{
			super.width = value;
			super.height = value;
		}

		override public function set height(value:Number):void
		{
			super.width = value;
			super.height = value;
		}

		public function set color(color:String):void
		{
			this._color = color;
			updateSource();
		}

		public function get color():String
		{
			return this._color;
		}

		private function updateSource():void
		{
			if (color != null)
			{
				this.source = FarmElement.getFarmElementImagePath(FarmElement.STABLE, color);
			}
		}

		private static function dragIt(event:MouseEvent):void
		{
			// Get the drag initiator component from the event object.
			var dragInitiator:StableImage = event.currentTarget as StableImage;

			// Create a DragSource object.
			var dragSource:DragSource = new DragSource();

			// Add the data to the object.
			dragSource.addData(dragInitiator, 'stable');

			// Create a copy of the image to use as a drag proxy. Move only one peep at a time
			var dragProxy:StableImage = new StableImage(dragInitiator.color);
			dragProxy.width = dragInitiator.width;

			// Call the DragManager doDrag() method to start the drag.
			DragManager.doDrag(dragInitiator, dragSource, event, dragProxy);
		}
	}
}